<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Files_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    
    function get_files($eid)
    {
        $sql = "SELECT f.* FROM files f LEFT JOIN entries_files ef ON f.photo_id=ef.files_id WHERE ef.eid = ?";
        $quiry = $this->db->query($sql, array($eid));
        if($quiry->num_rows() > 0)
        {
            return $quiry->result_array();
        }
        return ;
    }
    
    /**
     * 更新 merchandise_files 
     *
     * @access global
     * @param mixed $mer_id Entries表ID
     * @param mixed $tags 要写的tag
     * @return void
     */
    function add_merchandise_files($mer_id, $file_id)
    {
        /** 清除关联 */
        $this->delete_merchandise_files($mer_id);
        
        /** 写入关联 */
        $insert_entries_files = array();
        if(is_array($file_id))
        {
            $file_id = array_unique($file_id);
            foreach($file_id as $fid)
            {
                $insert_entries_files[] = array('mer_id'=>$mer_id,'files_id'=>$fid);
            }
        }
        else
        {
            $insert_entries_files[] = array('mer_id'=>$mer_id,'files_id'=>$file_id);
        }
        return $this->db->insert_batch('entries_files', $insert_entries_files);
    }
    
    /**
     * 更新 entries_files表
     *
     * @access global
     * @param mixed $eid Entries表ID
     * @param mixed $tags 要写的tag
     * @return void
     */
    function add_entries_files($eid, $file_id)
    {
        /** 清除关联 */
        $this->delete_entries_files($eid);
        
        /** 写入关联 */
        $insert_entries_files = array();
        if(is_array($file_id))
        {
            $file_id = array_unique($file_id);
            foreach($file_id as $fid)
            {
                $insert_entries_files[] = array('eid'=>$eid,'files_id'=>$fid);
            }
        }
        else
        {
            $insert_entries_files[] = array('eid'=>$eid,'files_id'=>$file_id);
        }
        return $this->db->insert_batch('entries_files', $insert_entries_files);
    }

    /** 清除关联 */
    public function delete_merchandise_files($eid)
    {
       return $this->db->delete('entries_files', array('eid' => $eid));
    }
    
    /** 清除关联 */
    public function delete_entries_files($eid)
    {
       return $this->db->delete('entries_files', array('eid' => $eid));
    }
}